package com.fivefu.core.hik.services.impl;

import com.alibaba.fastjson.JSON;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.hik.constant.HikRequestTypeEnum;
import com.fivefu.core.hik.dto.request.PageReq;
import com.fivefu.core.hik.dto.response.ResponsePage;
import com.fivefu.core.hik.dto.response.ResponseResult;
import com.fivefu.core.hik.entity.DbHikTree;
import com.fivefu.core.hik.services.DbHikTreeService;
import com.fivefu.core.hik.services.HikvisionService;
import com.fivefu.core.hik.services.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TreeServiceImpl implements TreeService {

    @Autowired
    private HikvisionService hikvisionService;
    @Autowired
    private DbHikTreeService dbHikTreeService;

    /**
     * 获取树结构
     */
    public void getHikTreeList(){
        List<Long> treeList = new ArrayList<>();
        String body = "{}";
        //查询类型枚举
        HikRequestTypeEnum typeEnum = HikRequestTypeEnum.TREECODE;

        //调用接口
        ResponseResult result = hikvisionService.doPostString(typeEnum.getUrl(), body, null, null, typeEnum.getType());
        if (result != null && "0".equals(result.getCode())){//调用成功
            String resultData = result.getData().toString();
            if (StrUtils.isNotNull(resultData)){
                ResponsePage resultPage = JSON.parseObject(resultData, ResponsePage.class);

                resultPage.getList().forEach(obj -> {
                    DbHikTree tree = JSON.parseObject(JSON.toJSONString(obj),DbHikTree.class);
                    //编辑监控点信息
                    Long treeId = dbHikTreeService.editHikTree(tree);
                    treeList.add(treeId);
                });

            }
        }
        dbHikTreeService.deletOtherHikTree(treeList);
    }

}
